#property copyright "Copyright 2012, Eric"
#property link      "mingxinhe@gmail.com"

#property indicator_chart_window
//----
#define NONOE -1
#define Blue1ColourSignal 1
#define Blue2ColourSignal 2
#define Red1ColourSignal 3
#define Red2ColourSignal 4

extern string CurrencyList = "EURUSD,GBPUSD,USDCHF,USDCAD,USDJPY,AUDUSD,NZDUSD";

extern int WPRPeriod = 14;
extern int RSIPeriod = 14;
extern double UpperLevel.WPR = 80; 
extern double UpperLevel.RSI = 80; 
extern double LowerLevel.WPR = 20; 
extern double LowerLevel.RSI = 20; 
extern bool Text.Alert = true;
extern bool Sound.Alert = true;
extern bool Email.Alert = true;
extern string SoundFileName = "Alert.wav";

#define NUM_TF 7

int X = 300,Y =20;
int barCount[][NUM_TF];
string symbols[];
datetime lastCheck2ndBlueColourTime, lastCheck2ndRedColourTime;

bool Blue2ColourWPRFirstCondition = false;
bool Red2ColourWPRFirstCondition = false;
bool Blue2ColourRSIFirstCondition = false;
bool Red2ColourRSIFirstCondition = false;

bool CurBlue2ColourWPRFirstCondition = false;
bool CurRed2ColourWPRFirstCondition = false;
bool CurBlue2ColourRSIFirstCondition = false;
bool CurRed2ColourRSIFirstCondition = false;

bool CurBlue2ColourWPRSecondCond = false;
bool CurRed2ColourWPRSecondCond = false;
bool CurBlue2ColourRSISecondCond = false;
bool CurRed2ColourRSISecondCond = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{       
    SplitString(CurrencyList, ",", symbols);
    ArrayResize(barCount, ArraySize(symbols));
       
    //int SY,I,Y=31+U,X=23+L,Y2=Y-19,X2=X+72,X3=X2+2,Y3=Y;SY=Y3;
    for(int i=0;i<ArraySize(symbols);i++)
    {
         string tex= symbols[i] + "_WR_RSI";   
         ObjectCreate(tex,OBJ_LABEL,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);
         ObjectSet(tex,OBJPROP_YDISTANCE,Y);
         ObjectSetText(tex,symbols[i],10,"Arial",Orange);
         Y+=40;
    }
    

   /*
    {
         tex= "M5EricHe";
         ObjectCreate(tex,23,0,0,0);
         ObjectSet(tex,OBJPROP_XDISTANCE,X);ObjectSet(tex,OBJPROP_YDISTANCE,Y);Y+=50;
         ObjectSetText(tex,"M5",12,"Arial",DeepSkyBlue);
    }
   */
   /*
   for(i=0;i<ArraySize(symbols);i++)
   {

       {
           tex=symbols[i]+"_M5";
           ObjectCreate(tex,23,0,0,0);
           ObjectSet(tex,102,X3);ObjectSet(tex,103,Y3);Y3+=50;
           ObjectSetText(tex,"",12,"Times New Roman",DarkViolet);
       }
      
       //Y3+=40;X3=SX+2;
       X3+=80;Y3=SY;
   }
   */
   
    Blue2ColourWPRFirstCondition = false;
    Red2ColourWPRFirstCondition = false;
    Blue2ColourRSIFirstCondition = false;
    Red2ColourRSIFirstCondition = false;
   
    CurBlue2ColourWPRFirstCondition = false;
    CurRed2ColourWPRFirstCondition = false;
    CurBlue2ColourRSIFirstCondition = false;
    CurRed2ColourRSIFirstCondition = false;

    CurBlue2ColourWPRSecondCond = false;
    CurRed2ColourWPRSecondCond = false;
    CurBlue2ColourRSISecondCond = false;
    CurRed2ColourRSISecondCond = false;
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
    ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
    for(int i=0;i<ArraySize(symbols);i++)
    {   
        int Pattern = -1;
        if (barCount[i][0]!=iBars(symbols[i],Period()))
        {
            //Pattern = getPattern(1, symbols[i], PERIOD_M5);
            int timeFrame = Period();
            barCount[i][0] = iBars(symbols[i],Period());
            //doAlert(symbols[i], timeFrame, Pattern);            
           // ObjectSetText(symbols[i]+"_M5",getAbrPatternName(Pattern),12,"Times New Roman",Red);
        }
     
    }  
}

void doAlert(string symbol, int timeFrame, int Pattern)
{
      if (Pattern < 0) return;
      string alertStr = "Pair:" + symbol + ", T/F:" +  getTimeFrame(timeFrame) + ", Pattern:";    //+ getPatternName(Pattern);                   
      if (Text.Alert)
      {
          Alert(alertStr);
          Print(alertStr);
      }
      if (Email.Alert)
      {
          SendMail("Candle Pattern Alert" , alertStr);  
      }
      if (Sound.Alert)
      {
          PlaySound(SoundFileName);
      }   
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string getTimeFrame(int tf)
{
   switch(tf)
   {
      case 1: return("M1");
      case 5: return("M5");
      case 15: return("M15");
      case 30: return("M30");
      case 60: return("H1");
      case 240: return("H4");
      case 1440: return("D1");
      case 10080: return("W1");
      case 43200: return("MN");
   }
}

// *************************************************************************************
// Service function for splitting a string into an array of values. For example,
// it can be used to take a comma-separated line (read using ReadLineFromFile() etc)
// and break it into an array of its comma-delimited parts.
// Example usage is:
//      string Results[];
//      StringSplit("abc,def,ghi", ",", Results);
// *************************************************************************************
bool SplitString(string stringValue, string separatorSymbol, string& results[], int expectedResultCount = 0)
{
   if (StringFind(stringValue, separatorSymbol) < 0)
   {// No separators found, the entire string is the result.
      ArrayResize(results, 1);
      results[0] = stringValue;
   }
   else
   {   
      int separatorPos = 0;
      int newSeparatorPos = 0;
      int size = 0;

      while(newSeparatorPos > -1)
      {
         size = size + 1;
         newSeparatorPos = StringFind(stringValue, separatorSymbol, separatorPos);
         
         ArrayResize(results, size);
         if (newSeparatorPos > -1)
         {
            if (newSeparatorPos - separatorPos > 0)
            {  // Evade filling empty positions, since 0 size is considered by the StringSubstr as entire string to the end.
               results[size-1] = StringSubstr(stringValue, separatorPos, newSeparatorPos - separatorPos);               
            }
         }
         else
         {  // Reached final element.
            results[size-1] = StringSubstr(stringValue, separatorPos, 0);
         }         
         results[size-1] = StringTrimLeft(StringTrimRight(results[size-1]));
         
         separatorPos = newSeparatorPos + 1;
      }
   }   
   
   if (expectedResultCount == 0 || expectedResultCount == ArraySize(results))
   {  // Results OK.
      return (true);
   }
   else
   {  // Results are WRONG.
      Print("ERROR - size of parsed string not expected.");
      return (false);
   }
}

int ColourOneSignal(int shift,string symbol,int timeframe)
{
    if (iWPR(NULL,0,WPRPeriod,0) > UpperLevel.WPR && iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) > LowerLevel.RSI)
    {
        return (Blue1ColourSignal);
    }  
    
    if (iWPR(NULL,0,WPRPeriod,0) < LowerLevel.WPR && iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) < UpperLevel.RSI)
    {
        return (Red1ColourSignal);
    }  
   
   return (NONOE);
}

bool BlueColourTwoSignal(int shift,string symbol,int timeframe)
{
    if (Time[0] > lastCheck2ndBlueColourTime)
    {
        CurBlue2ColourWPRFirstCondition = Blue2ColourWPRFirstCondition;
        CurBlue2ColourRSIFirstCondition = Blue2ColourRSIFirstCondition;
    
        Blue2ColourWPRFirstCondition = false;
        Blue2ColourRSIFirstCondition = false;
        
        CurBlue2ColourWPRSecondCond = false;        
        CurBlue2ColourRSISecondCond = false;        
        
        lastCheck2ndBlueColourTime = Time[0];
    }
    
    if (iWPR(NULL,0,WPRPeriod,0) < LowerLevel.WPR )
    {
        Blue2ColourWPRFirstCondition = true;
        CurBlue2ColourWPRFirstCondition = true;
    }  
    
    if (CurBlue2ColourWPRFirstCondition && iWPR(NULL,0,WPRPeriod,0) > UpperLevel.WPR)
    {
        CurBlue2ColourWPRSecondCond = true; 
    }
    
    if (iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) < LowerLevel.RSI )
    {
        Blue2ColourRSIFirstCondition = true;
        CurBlue2ColourRSIFirstCondition = true;
    }  
    
    if (CurBlue2ColourRSIFirstCondition && iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) > LowerLevel.RSI)
    {
        CurBlue2ColourRSISecondCond = true;   
    }

    if (CurBlue2ColourWPRFirstCondition && CurBlue2ColourWPRSecondCond && CurBlue2ColourRSIFirstCondition && CurBlue2ColourRSISecondCond) 
    {
        return (true);
    }
    
    return (false);   
}

bool RedColourTwoSignal(int shift,string symbol,int timeframe)
{
    if (Time[0] > lastCheck2ndRedColourTime)
    {
        CurRed2ColourWPRFirstCondition = Red2ColourWPRFirstCondition;
        CurRed2ColourRSIFirstCondition = Red2ColourRSIFirstCondition;
    
        Red2ColourWPRFirstCondition = false;
        Red2ColourRSIFirstCondition = false;
               
        CurRed2ColourWPRSecondCond = false;        
        CurRed2ColourRSISecondCond = false;
        
        lastCheck2ndRedColourTime = Time[0];
    }
    
    if (iWPR(NULL,0,WPRPeriod,0) > UpperLevel.WPR )
    {
        Red2ColourWPRFirstCondition = true;
        CurRed2ColourWPRFirstCondition = true;
    }  
    
    if (CurRed2ColourWPRFirstCondition && iWPR(NULL,0,WPRPeriod,0) < LowerLevel.WPR)
    {
        CurRed2ColourWPRSecondCond = true; 
    }
    
    if (iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) > UpperLevel.RSI )
    {
        Red2ColourRSIFirstCondition = true;
        CurRed2ColourRSIFirstCondition = true;
    }  
    
    if (CurRed2ColourRSIFirstCondition && iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0) < UpperLevel.RSI)
    {
        CurRed2ColourRSISecondCond = true;   
    }

    if (CurRed2ColourWPRFirstCondition && CurRed2ColourWPRSecondCond && CurRed2ColourRSIFirstCondition && CurRed2ColourRSISecondCond) 
    {
        return (true);
    }
    
    return (false);   
}